package com.xy.mainp.main.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.xy.mainp.R;
import com.xy.mainp.base.TittleBarFragmnet;
import com.xy.mainp.databinding.FragmentNovelBinding;
import com.xy.mainp.huaban.fragment.ImagFragment;
import com.xy.mainp.model.biqu.BiquCategoryEntity;
import com.xy.mainp.novel.widget.NovelFragmentHelper;
import com.xy.mainp.utils.NetRequestUtils;
import com.xy.net.OkHttpUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

/**
 * Created by John on 2016/10/28.
 */

public class NovelFragment extends TittleBarFragmnet {

    private List<BiquCategoryEntity> categoryList;
    private FragmentNovelBinding binding;
    private NovelVpAdapter novelVpAdapter;

    @Override
    protected void onIn() {
        super.onIn();
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_novel, null, true);
        setContentView(binding.getRoot());
        initView();
    }

    @Override
    protected void onShow() {
        super.onShow();
        loadNetData();
    }

    private void initView() {
        categoryList = new ArrayList<>();
        novelVpAdapter = new NovelVpAdapter(getFragmentManager());
        binding.vpNovel.setAdapter(novelVpAdapter);
        binding.tlNovel.setViewPager(binding.vpNovel);
    }

    private void loadNetData() {
        showLoading(true);
        NetRequestUtils.getInstance(getContext()).getBiQuData(new OkHttpUtils.ResultCallBack<String>() {
            @Override
            public void onError(Request request, Exception e) {
                showLoading(false);
                showToastMsg(e.getMessage());
            }

            @Override
            public void onResult(String result) {
                showLoading(false);
                final Document document = Jsoup.parse(result);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        CategoryParse(document);
                    }
                }).start();
            }
        });
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            binding.vpNovel.removeAllViews();
            novelVpAdapter.setData(categoryList);
            novelVpAdapter.notifyDataSetChanged();
        }
    };
    private void CategoryParse(Document document) {
        Elements elements = document.select("div.nav ul li");
        for (int i = 0; i < elements.size(); i++) {
            BiquCategoryEntity entity = new BiquCategoryEntity();
            if (elements.get(i).text().equals("首页")) {
                entity.setSelected(true);
            }
            entity.setName(elements.get(i).text());
            entity.setUrl(elements.get(i).select("a").attr("href"));
            categoryList.add(entity);
        }
        handler.obtainMessage().sendToTarget();
    }

    class NovelVpAdapter extends FragmentStatePagerAdapter {

        private List<BiquCategoryEntity> mData;
        private Fragment mCurrentFragment;

        public NovelVpAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setData(List<BiquCategoryEntity> mData) {
            this.mData = mData;
        }

        @Override
        public Fragment getItem(int position) {
            return NovelFragmentHelper.getInstance(getActivity()).getFragment(mData,position);
        }

        @Override
        public int getCount() {
            return mData == null ? 0 : mData.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mData.get(position).getName();
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            this.mCurrentFragment = (Fragment) object;
        }

        public Fragment getPrimaryItem() {
            return mCurrentFragment;
        }
    }


}
